package com.ruoyi.carbon.service.impl;

import com.ruoyi.carbon.domain.CheliangDiaodu;
import com.ruoyi.carbon.domain.CheliangDiaoduDetail;
import com.ruoyi.carbon.domain.dto.CheliangDiaoduDto;
import com.ruoyi.carbon.domain.vo.CheliangDiaoduVo;
import com.ruoyi.carbon.mapper.CheliangDiaoduDetailMapper;
import com.ruoyi.carbon.mapper.CheliangDiaoduMapper;
import com.ruoyi.carbon.service.CheliangDiaoduService;
import com.ruoyi.common.exception.ServiceException;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CheliangDiaoduServiceImpl implements CheliangDiaoduService {


    @Autowired
    private CheliangDiaoduMapper cheliangDiaoduMapper;

    @Autowired
    private CheliangDiaoduDetailMapper cheliangDiaoduDetailMapper;

    @Override
    public List<CheliangDiaodu> getList(CheliangDiaodu cheliangDiaodu) {
        return cheliangDiaoduMapper.getList(cheliangDiaodu);
    }

    @Override
    @Transactional
    public int save(CheliangDiaoduVo cheliangDiaoduVo) {
        CheliangDiaodu cheliangDiaodu = cheliangDiaoduVo.getCheliangDiaodu();
        if (cheliangDiaodu.getChengyunNumber() == null) {
            throw new ServiceException("承运单号不能为空");
        }
        cheliangDiaoduMapper.save(cheliangDiaodu);
        List<CheliangDiaoduDetail> cheliangDiaoduDetails = cheliangDiaoduVo.getCheliangDiaoduDetails();
        cheliangDiaoduDetails = cheliangDiaoduDetails.stream().map(item -> {
            item.setCheliangDiaoduId(cheliangDiaodu.getId());
            return item;
        }).collect(Collectors.toList());

        if (ObjectUtils.isNotEmpty(cheliangDiaoduDetails)) cheliangDiaoduDetailMapper.batchInsert(cheliangDiaoduDetails);

        return 1;
    }

    @Override
    public CheliangDiaoduVo getAllInfoById(Long id) {
        CheliangDiaodu cheliangDiaodu = cheliangDiaoduMapper.getById(id);
        List<CheliangDiaoduDetail> cheliangDiaoduDetails = cheliangDiaoduDetailMapper.getByCLDDId(id);
        CheliangDiaoduVo cheliangDiaoduVo = new CheliangDiaoduVo();
        cheliangDiaoduVo.setCheliangDiaodu(cheliangDiaodu);
        cheliangDiaoduVo.setCheliangDiaoduDetails(cheliangDiaoduDetails);
        return cheliangDiaoduVo;
    }

    @Override
    @Transactional
    public int update(CheliangDiaoduVo cheliangDiaoduVo) {

        CheliangDiaodu cheliangDiaodu = cheliangDiaoduVo.getCheliangDiaodu();
        if (cheliangDiaodu.getChengyunNumber() == null) {
            throw new ServiceException("承运单号不能为空");
        }
        List<CheliangDiaoduDetail> cheliangDiaoduDetails = cheliangDiaoduVo.getCheliangDiaoduDetails();
        Long id = cheliangDiaodu.getId();

        // 1. 往明细表中加数据

        cheliangDiaoduDetailMapper.deleteByClddId(id);
        if (ObjectUtils.isNotEmpty(cheliangDiaoduDetails)) {
            cheliangDiaoduDetails = cheliangDiaoduDetails.stream().peek(item -> item.setCheliangDiaoduId(id)).collect(Collectors.toList());
            cheliangDiaoduDetailMapper.batchInsert(cheliangDiaoduDetails);

        }

        // 2. 修改

        return cheliangDiaoduMapper.update(cheliangDiaodu);
    }
}
